{extend name='table'}

{block name="button"}

<!--{if auth("add")}-->
<button data-modal='{:url("add")}' data-area="['950px', '850px']" data-title="{:lang('添加课程')}" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('添加课程')}</button>
<!--{/if}-->

{/block}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <div class="layui-tab-content">
        {include file='task/index_search'}
        <table id="UserTable" data-url="{:sysuri('index')}" data-target-search="form.form-search"></table>
    </div>
</div>
<script>
    var maxWidth = window.innerWidth;
    var maxHeight = window.innerHeight;

    var account_show = {$account_show};

    $(function () {
        $('#UserTable').layTable({
            even: true, height: 'full',
            where: {type: '{$type|default="index"}'},
            cols: [[
                {checkbox: true, fixed: true},

                {field: 'account_name', title: '{:lang("学校")}', minWidth: 150, align: 'center', hide:!account_show },
                {field: 'user_name', title: '{:lang("老师")}', minWidth: 100, align: 'center'  },
                {
                    field: 'logo', title: '{:lang("封面图")}', width: 100, align: 'center', templet: function (d) {
                        if (!d.logo) return '-';
                        return layui.laytpl('<div class="headimg headimg-ss shadow-inset margin-0" data-tips-image data-tips-hover data-lazy-src="{{d.logo}}"></div>').render(d);
                    }
                },
                {field: 'title', title: '{:lang("标题")}', minWidth: 200, align: 'center', templet: '<div>{{d.title||"-"}}</div>'},
                {field: 'details', title: '{:lang("简介")}', minWidth: 300, align: 'center', templet: '<div>{{d.details||"-"}}</div>'},
                {field: 'status', title: '{:lang("使用状态")}', align: 'center', minWidth: 120, templet: '#StatusSwitchTpl'},
                {field: 'create_at', title: '{:lang("创建时间")}', align: 'center', minWidth: 150, sort: true},
                {toolbar: '#toolbar', title: '{:lang("操作面板")}', align: 'center', minWidth: 250, fixed: 'right'}
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            var data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserTable').trigger('reload');
                }); else {
                    $('#UserTable').trigger('reload')
                }
                return false;
            }, false);
        });

        // 指向元素为 `<select lay-filter="test"></select>` 的选择事件
        form.on('select(platform)', function(data){

            $.ajax({
                type : 'get',
                url  : "{:sysuri('common/ajax/getTeacherList')}",
                data : {account_id:data.value},
                success:function (res) {
                    if (res.code == 1) {
                        var html = "<option value=''>{:lang('-- 请选择 --')}</option>";

                        let data = res.data;
                        for (var i in data) {

                            html += "<option value='"+i+"'>"+data[i]+"</option>";
                        }

                        $('.teacher').html(html);

                        layui.form.render('select'); // 仅渲染 select 元素
                    }
                }
            })
        });
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("state")}-->
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('已激活')}|{:lang('已禁用')}" lay-filter="StatusSwitch" {{-d.status>0?'checked':''}}>
    <!--{else}-->
    {{-d.status ? '<b class="color-green">{:lang("已激活")}</b>' : '<b class="color-red">{:lang("已禁用")}</b>'}}
    <!--{/if}-->
</script>

<script type="text/html" id="toolbar">

    <!--{if auth("edit")}-->
    <a class="layui-btn layui-btn-sm"   data-title="{:lang('编辑用户')}" data-area="['950px', '850px']" data-modal='{:url("edit")}?id={{d.id}}'>{:lang('编 辑')}</a>
    <!--{/if}-->
    <!--{if auth("task_chapter/edit")}-->
    <a class="layui-btn layui-btn-sm" data-offset="['60px','200px']" data-area="['{{maxWidth-210}}px', '{{maxHeight-60}}px']" data-title="【<span style='color:red;'>{{d.title}}</span>】{:lang('添加章节')}" data-modal='{:url("task_chapter/index")}?task_id={{d.id}}'>{:lang('添加章节')}</a>
    <!--{/if}-->
    <!--{if auth("remove")}-->
    <a class="layui-btn layui-btn-sm layui-btn-danger" data-confirm="{:lang('确定要永久删除吗？')}" data-action="{:url('remove')}" data-value="id#{{d.id}}">{:lang('删 除')}</a>
    <!--{/if}-->

</script>
{/block}
